// pages/company/details/details.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    itemDetail: "",
    volume:'',
    volumeWeight:'',
    isCal:false,
    tip:'',
    id:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that =this
    that.setData({
      id: options.id
    })
    var company = wx.getStorageSync("COMPANY");
    app.getApisj.request(
      "/api/Orders/Detail?id=" + options.id, {
      },
      "GET",
      res => {
        wx.hideLoading();
        if (res.data.responseCode == 1000) {
          //列表数据
          console.log(res.data.obj)
          var orderGoodInfosList = res.data.obj.orderGoodInfosList
          var volume=0, volumeWeight=0
          if (orderGoodInfosList.length){
            for (var i = 0; i < orderGoodInfosList.length;i++){
              volume += orderGoodInfosList[i].volume
              volumeWeight += orderGoodInfosList[i].volumeWeight
            }
          }
          that.setData({
            itemDetail: res.data.obj ,
            volume: Number(volume),
            volumeWeight: Number(volumeWeight) 
          });
          
        } else {
          wx.showToast({
            title: '请求失败',
            icon: 'loading',
            duration: 2000
          });
        }

      },
      res => { }
    );


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  return(){//退回
    var that = this
    that.setData({
      status:1,
      isCal:true,
      tip:"是否退回订单"
    })
  },
 yes() {//接单
    var that = this
    that.setData({
      status: 2,
      isCal: true,
      tip: "是否确认接单"
    })
  },
  cal(){//取消
  var that = this
    that.setData({
      isCal: false,
    })   
  },
  sub(){//确认
  var that = this
    if (that.data.status == 1){
      app.getApisj.request(
        "/api/Orders/ReturnBack?id=" + that.data.id, {
        },
        "POST",
        res => {
          wx.hideLoading();
          if (res.data.responseCode == 1000) {
            //列表数据
            console.log(res.data.obj)
            wx.showToast({
              title: '退回成功',
              icon: 'loading',
              duration: 2000,
            });
            setTimeout(() => {
              wx.switchTab({
                url: '../index/index',
              })
            }, 2000)
          } else {
            wx.showToast({
              title: '退回失败',
              icon: 'loading',
              duration: 2000
            });
          }

        },
        res => { }
      );      
    }
    if (that.data.status == 2) {
      app.getApisj.request(
        "/api/Orders/Take" , {
          id: that.data.id
        },
        "POST",
        res => {
          wx.hideLoading();
          if (res.data.responseCode == 1000) {
            //列表数据
            console.log(res.data.obj)
            wx.showToast({
              title: '接单成功',
              icon: 'loading',
              duration: 2000,
            });
            setTimeout(() => {
              wx.switchTab({
                url: '../index/index',
              })
            }, 2000)
          } else {
            wx.showToast({
              title: '接单失败',
              icon: 'loading',
              duration: 2000
            });
          }

        },
        res => { }
      );
    }
  },
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})